from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework.views import APIView


# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.viewsets import ViewSet
from utils.common_response import APIResponse
# from .serializer import LoginSerializer
from .serializer import LoginSerializer


class SimpleApiView(APIView):
    def get(self, request):
        data = {
            'message': 'Hello, World!',
            'status': 'success'
        }
        return Response(data, status=status.HTTP_200_OK)

    def post(self, request):
        data = request.data
        response_data = {
            'received_data': data,
            'message': 'Data received successfully'
        }
        return Response(response_data, status=status.HTTP_201_CREATED)


class LoginView(ViewSet):
    authentication_classes = []  # 局部禁用
    permission_classes = []
    throttle_classes = []

    @action(methods=['POST'], detail=False)
    def login(self, request):
        print(request.data)
        user_info = LoginSerializer(data=request.data)
        user_info.is_valid(raise_exception=True)
        user = user_info.context.get('user_info')
        token = user_info.context.get('token')
        # user_type = user_info.context.get('user_type')
        # gender = user_info.context.get('gender')
        # mobile = user_info.context.get('mobile')
        return APIResponse(msg='登录成功',
                           user_info=user,
                           token=token)
